<?php

namespace App\Console\Commands;

use App\Jobs\TaskJob;
use App\Jobs\TaskJob2;
use App\Libraries\Scheduler;
use App\Libraries\Task;
use Illuminate\Bus\Dispatcher;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class Xiecheng extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'run:process';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $scheduler = new Scheduler();
app(Dispatcher::class)->dispatch(new TaskJob($scheduler));
      //  app(Dispatcher::class)->dispatch(new TaskJob2($scheduler));
     /*   app()->bindMethod(TaskJob2::class.'@handle', function ($job, $app) use($scheduler) {
            return $job->handle($app->make(Scheduler::class));
        });*/
        app(Dispatcher::class)->dispatch(new TaskJob2($scheduler));
        Log::info(json_encode($scheduler->getQueue()));
/*$task1 = new Task(call_user_func(function() {
    for ($i = 0; $i < 3; $i++) {
      //  sleep(1);
        print "task1: " . $i . "\n";
        sleep(1);
        yield;
    }
}));

$task2 = new Task(call_user_func(function() {
    for ($i = 0; $i < 6; $i++) {
        print "task2: " . $i . "\n";
       yield;
    }
}));*/
/*$scheduler->setQueueModel();
$scheduler->enqueue($task1);
$scheduler->enqueue($task2);*/
        sleep(4);
        Log::info($scheduler->getQueue()->count());
$scheduler->run();
    //    $scheduler->run();

echo '123456';
    }
}
